/**
 * 
 */
package com.teach.servlet.admin;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teach.entity.User;
import com.teach.service.UserAddService;
import com.teach.service.impl.UserAddServiceImpl;

/**
 * @author Liulianqiu
 *2020年7月31日
 */
public class UserAddServlet extends HttpServlet {

	private UserAddService userAddService = new UserAddServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//给post请求的请求正文设置编码
		User user = new User();
		//1、获取参数
		user.setLlq_id(null);//设置默认属性值
		user.setLlq_name(request.getParameter("userName"));
		user.setLlq_age(Integer.valueOf(request.getParameter("age")));
		user.setLlq_birthday(Date.valueOf(request.getParameter("userBrithdate")));
		try {
			if(Boolean.valueOf(request.getParameter("userSex"))) {
				user.setLlq_sex("男");
			}else {
				user.setLlq_sex("女");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new RuntimeException("数据格式错误");
		}
		
		System.out.println(user);
		
		//2、调用业务逻辑层接口
		try {
			userAddService.add(user);
			request.setAttribute("msg", "添加用户成功");
			request.setAttribute("href", "admin/display-user.let");
		} catch (Exception e) {
			request.setAttribute("error", "添加用户失败:"+e.getMessage());
			e.printStackTrace();
		}
		//3、跳转视图
		request.getRequestDispatcher("/admin/msg.jsp").forward(request, response);
	}
}
